﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using YYTea.MOdel.Model;
using YYTea.ORM;
using YYTea.Services.IServices;
using YYTea.Services.VModel;

namespace YYTea.Services.Services
{
    public class OrderServices : IOrder
    {
        private readonly YYTeaContext _context;
        public OrderServices(YYTeaContext context)
        {
            this._context = context;
        }
        /// <summary>
        /// 添加或修改订单地址
        /// </summary>
        /// <param name="addRess"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<Order> AddAddress(OrderAddressCRUD addRess, Guid Id)
        {
            //throw new NotImplementedException();
            if (addRess == null)
            {
                return null;
            }
            var shopid = await _context.Orders.SingleOrDefaultAsync(s => s.ShoppingCarID == Id);
            if (shopid != null)
            {
                //Order order = new Order();
                //order.Addree = addRess.Address;
                shopid.Addree = addRess.Address;
                _context.Update(shopid);
                await _context.SaveChangesAsync();
                //return order;
                return shopid;
            }
            return null;

        }

        /// <summary>
        /// 删除订单地址
        /// </summary>
        /// <returns></returns>
        public async Task<Order> DeleteAddress(Guid Id)
        {
            if (Id == null)
            {
                return null;
            }
            var shopid = await _context.Orders.SingleOrDefaultAsync(s => s.ShoppingCarID == Id);
            if (shopid != null)
            {
                shopid.Addree = null;
                _context.Orders.Update(shopid);
                await _context.SaveChangesAsync();
                return shopid;
            }
            //throw new NotImplementedException();
            return null;

        }

        public Task<Order> UpdateAddress(string addRess)
        {
            throw new NotImplementedException();
        }

        public async Task<List<Order>> GetAllOrder()
        {
            //throw new NotImplementedException();
            var allOrder = await _context.Orders.ToListAsync();
            return allOrder;
        }

        public async Task<Order> GetOrderById(Guid id)
        {

            if (id == null)
            {
                return null;
            }
            var orderById = await _context.Orders.FirstOrDefaultAsync(d => d.OrderID == id);
            return orderById;
            //throw new NotImplementedException();
        }

        public Task<Order> DeleteOrder(Guid Id)
        {
            throw new NotImplementedException();
        }
    }
}
